package com.baozun.dev.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baozun.dev.R;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class PayDemoActivity extends Activity implements OnClickListener {
	private Button btn_pay;
	private RelativeLayout btn_alipay, btn_wxpay, btn_unionpay;
	private ImageView alipay_state, wxpay_state, unionpay_state;
	private TextView pay_price;

	private int payMethod;
	/** 支付方式：支付宝 */
	private static final int ALIPAY_METHOD = 1;
	/** 支付方式：微信 */
	private static final int WXPAY_METHOD = 2;
	/** 支付方式：银联 */
	private static final int UNIONPAY_METHOD = 3;
	
	/** 调用微信API前，需要先向微信注册您的APPID*/
	private final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
	
	/** 银联支付交易流水号 */
	private static final String TN_URL_01 = "http://101.231.204.84:8091/sim/getacptn";
	private ProgressDialog mLoadingDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_main);

		findViews();
		initViews();
		registerListener();
	}

	private void findViews() {
		btn_pay = (Button) findViewById(R.id.btn_pay);
		btn_alipay = (RelativeLayout) findViewById(R.id.btn_alipay);
		btn_wxpay = (RelativeLayout) findViewById(R.id.btn_wxpay);
		btn_unionpay = (RelativeLayout) findViewById(R.id.btn_unionpay);
		alipay_state = (ImageView) findViewById(R.id.alipay_state);
		wxpay_state = (ImageView) findViewById(R.id.wxpay_state);
		unionpay_state = (ImageView) findViewById(R.id.unionpay_state);
		pay_price = (TextView) findViewById(R.id.pay_price);
	}

	private void initViews() {
		selectPayMethod(ALIPAY_METHOD);
	}

	private void registerListener() {
		btn_pay.setOnClickListener(this);
		btn_alipay.setOnClickListener(this);
		btn_wxpay.setOnClickListener(this);
		btn_unionpay.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_pay:
			doPay(payMethod, pay_price.getText().toString());
			break;
		case R.id.btn_alipay:
			selectPayMethod(ALIPAY_METHOD);
			break;
		case R.id.btn_wxpay:
			selectPayMethod(WXPAY_METHOD);
			break;
		case R.id.btn_unionpay:
			selectPayMethod(UNIONPAY_METHOD);
			break;

		default:
			break;
		}
	}
	
	/**
	 * @param flag=1 支付宝  flag=2微信
	 */
	private void selectPayMethod(int flag){
		if(flag == ALIPAY_METHOD){
			payMethod = ALIPAY_METHOD;
			alipay_state.setImageResource(R.drawable.del_cancel_selected);
			wxpay_state.setImageResource(R.drawable.del_cancel_select);
			unionpay_state.setImageResource(R.drawable.del_cancel_select);
		}else if(flag == WXPAY_METHOD){
			payMethod = WXPAY_METHOD;
			alipay_state.setImageResource(R.drawable.del_cancel_select);
			wxpay_state.setImageResource(R.drawable.del_cancel_selected);
			unionpay_state.setImageResource(R.drawable.del_cancel_select);
		}else if(flag == UNIONPAY_METHOD){
			payMethod = UNIONPAY_METHOD;
			alipay_state.setImageResource(R.drawable.del_cancel_select);
			wxpay_state.setImageResource(R.drawable.del_cancel_select);
			unionpay_state.setImageResource(R.drawable.del_cancel_selected);
		}
	}
	
	public void doPay(int payMethod, String payPrice) {
		if(payMethod == WXPAY_METHOD){
			Toast.makeText(this, "微信支付暂未开放",Toast.LENGTH_SHORT).show();
		}else if(payMethod == ALIPAY_METHOD){
			Toast.makeText(this, "支付宝支付暂未开放",Toast.LENGTH_SHORT).show();
		}else if(payMethod == UNIONPAY_METHOD){
			Toast.makeText(this, "银联支付暂未开放",Toast.LENGTH_SHORT).show();
		}
	}

}
